package com.lxj.student_security.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.student_security.common.CommonResult;
import com.lxj.student_security.entity.Daily;
import com.lxj.student_security.entity.User;
import com.lxj.student_security.mapper.DailyMapper;
import com.lxj.student_security.service.DailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DailyServiceImpl implements DailyService {
    @Autowired
    private DailyMapper dailyMapper;
    @Override
    public CommonResult findTeacher() {
        List<User> list = dailyMapper.findTeacher();
        return CommonResult.success(list);
    }

    @Override
    public CommonResult save(Daily daily) {
        int count = dailyMapper.save(daily);
        if (count==1){
            return CommonResult.success(count);
        }
        return CommonResult.failed("保存失败");
    }

    @Override
    public CommonResult findAll(Daily daily) {
        PageHelper.startPage(daily.getPageNum(),daily.getPageSize());
        PageInfo<Daily> info = new PageInfo<>(this.dailyMapper.findAll(daily));
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",info.getList());
        map.put("number",info.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult delete(Integer[] id) {
        int number = dailyMapper.delete(id);
        if (number==0){
            return CommonResult.failed();
        }
        return CommonResult.success(number);
    }

    @Override
    public CommonResult edit(Daily daily) {
        int number = dailyMapper.edit(daily);
        if (number==0){
            return CommonResult.failed();
        }
        return CommonResult.success(number);
    }

    @Override
    public CommonResult submit(Integer id) {
        int number = dailyMapper.submit(id);
        if (number==0){
            return CommonResult.failed();
        }
        return CommonResult.success(number);
    }

    @Override
    public CommonResult findById(Integer id) {
        Daily daily = dailyMapper.findById(id);
        return CommonResult.success(daily);
    }

    @Override
    public CommonResult query(Integer id) {
        Daily daily = dailyMapper.findById((id));
        return CommonResult.success(daily);
    }

    @Override
    public CommonResult searchAll(Daily daily) {
        PageHelper.startPage(daily.getPageNum(),daily.getPageSize());
        PageInfo<Daily> info = new PageInfo<>(this.dailyMapper.searchAll(daily));
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",info.getList());
        map.put("number",info.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult audit(Integer id) {
        int number = dailyMapper.audit(id);
        if (number==1){
            return CommonResult.success(number);
        }
        return CommonResult.failed("审核失败");
    }
}
